package com.ysd.lis.controller;


import cn.hutool.core.io.FileUtil;
import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.WkKnowledge;
import com.ysd.lis.request.KnowledgeDto;
import com.ysd.lis.request.KnowledgePageParam;
import com.ysd.lis.service.WkKnowledgeService;
import com.ysd.util.PropertySlash;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 员工表
 * 前端控制器
 * </p>
 *
 * @author peng
 * @since 2024-03-12
 */
@Api(tags = "知识库")
@RestController
@RequestMapping("/api/wk/wkKnowledge")
public class WkKnowledgeController extends BaseController {

    @Value("${picturePath}")
    private String picturePath;
    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @Autowired
    WkKnowledgeService wkKnowledgeService;

    @ApiOperation("分页查询")
    @GetMapping("/findKnowledgePageList")
    public Result findKnowledgePageList(KnowledgePageParam pageParam){
        return wkKnowledgeService.findKnowledgePageList(pageParam);
    }

    @ApiOperation("不分页查询")
    @GetMapping("/findKnowledgeList")
    public Result findKnowledgeList(){
        return wkKnowledgeService.findKnowledgeList();
    }

    @ApiOperation("查询知识库🌲")
    @GetMapping("/getKnowledgeTree")
    public Result getKnowledgeTree(){
        return wkKnowledgeService.getKnowledgeTree();
    }


    @ApiOperation("新增")
    @PostMapping("/addKnowledge")
    public Result addKnowledge(@RequestBody WkKnowledge knowledge){
        return  wkKnowledgeService.addKnowledge(knowledge);
    }

    @ApiOperation("修改")
    @PostMapping("/editKnowledge")
    public Result editKnowledge(@RequestBody WkKnowledge knowledge) {
        return wkKnowledgeService.editKnowledge(knowledge);
    }


    @ApiOperation("批量删除")
    @PostMapping("/batchDeleteKnowledgeList")
    public Result batchDeleteKnowledgeList(@RequestBody KnowledgeDto dto){
        return  wkKnowledgeService.batchDeleteKnowledgeList(dto);
    }

    @ApiOperation("上传图片")
    @PostMapping("/uploadKnowledgeImg")
    public Result uploadKnowledgeImg(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String id = request.getParameter("id");
        if (file == null) {
            return Result.fail("文件不能为空");
        }
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = picturePath + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + file.getOriginalFilename();
        String path = folder + fileName;
        File files = new File(path);
        if (FileUtil.exist(files)) {
            return Result.fail("文件已存在");
        }
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        return wkKnowledgeService.uploadKnowledgeImg(id, path);
    }

}
